import 'package:fig/sqlite/sql_manager.dart';
import 'package:fig/sqlite/table_media.dart';

class Media {
  int id;
  int originId;
  String name;
  String subName;
  String cover;
  String description;
  int categoryId;
  int doubanId;
  int doubanRank;
  int upNum;
  int downNum;
  String director;
  String actor;
  String tag;
  int year;
  String area;
  String lang;
  int pubDate;
  String imdb;
  String playData;
  int createTime;

  Media({this.id, this.originId, this.name, this.subName, this.cover, this.description, this.categoryId, this.doubanRank, this.upNum, this.downNum, this.director, this.actor, this.tag, this.year, this.area, this.lang, this.pubDate, this.imdb, this.playData, this.createTime});

  // 修复数据，为入库做准备
  Media fix() {
    if (this.description != null && this.description != '') {
      this.description = this.description.replaceAll(RegExp(r'<(.+?)>'), '').trim();
    }

    fixActor();
    fixDirector();
    fixArea();
    fixLang();

    return this;
  }

  String commonFix(String value) {
    value = value.replaceAll(' / ', ',').replaceAll('/ ', ',').replaceAll(' /', ',').replaceAll('/', ',');
    value = value.replaceAll(' \\ ', ',').replaceAll('\\ ', ',').replaceAll(' \\', ',').replaceAll('\\', ',');
    value = value.replaceAll(' 、 ', ',').replaceAll('、 ', ',').replaceAll(' 、', ',').replaceAll('、', ',');
    value = value.replaceAll(' ， ', ',').replaceAll('， ', ',').replaceAll(' ，', ',').replaceAll('，', ',');
    value = value.replaceAll(' , ', ',').replaceAll(', ', ',').replaceAll(' ,', ',');
    value = value.replaceAll(' ', ',');
    return value;
  }

  // 修复演员数据
  void fixActor() {
    if (this.actor == null || this.actor == '' || this.actor.contains('内详')) {
      this.actor = '';
      return;
    }

    String fixBefore = this.actor;
    this.actor = commonFix(this.actor);

    // print('actor 修复前[$fixBefore]->修复后[${this.actor}]');
  }

  // 修复演员数据
  void fixDirector() {
    if (this.director == null || this.director == '' || this.director.contains('内详')) {
      this.director = '';
      return;
    }

    String fixBefore = this.director;
    this.director = commonFix(this.director);

    // print('director 修复前[$fixBefore]->修复后[${this.director}]');
  }

  // 修复地域数据
  void fixArea() {
    if (this.area == null || this.area == '') {
      return;
    }

    String fixBefore = this.area;
    this.area = commonFix(this.area);

    var _areas = this.area.split(',');

    for (int i = 0; i < _areas.length; i++) {
      if (_areas[i].contains('中国香港')) {
        _areas[i] = '香港';
      } else if (_areas[i].contains('中国大陆')) {
        _areas[i] = '大陆';
      } else if (_areas[i].contains('中国香港')) {
        _areas[i] = '香港';
      } else if (_areas[i].contains('中国台湾')) {
        _areas[i] = '台湾';
      } else if (_areas[i].contains('其他') || _areas[i].contains('其它')) {
        _areas[i] = '其他';
      } else if (_areas[i].contains('Russia') || _areas[i].contains('俄国')) {
        _areas[i] = '俄罗斯';
      } else if (_areas[i].contains('Australia') || _areas[i].contains('澳大利亚')) {
        _areas[i] = '澳大利亚';
      } else if (_areas[i].contains('Belgium') || _areas[i].contains('比利时')) {
        _areas[i] = '比利时';
      } else if (_areas[i].contains('Canada') || _areas[i].contains('比利时')) {
        _areas[i] = '加拿大';
      } else if (_areas[i].contains('India') || _areas[i].contains('印度')) {
        _areas[i] = '印度';
      } else if (_areas[i].contains('Ireland') || _areas[i].contains('爱尔兰')) {
        _areas[i] = '爱尔兰';
      } else if (_areas[i].contains('Switzerland') || _areas[i].contains('瑞士')) {
        _areas[i] = '瑞士';
      } else if (_areas[i].contains('Cuba') || _areas[i].contains('古巴')) {
        _areas[i] = '古巴';
      } else if (_areas[i].contains('Indonesia') || _areas[i].contains('印度尼西亚')) {
        _areas[i] = '印度尼西亚';
      } else if (_areas[i].contains('Germany') || _areas[i].contains('德国')) {
        _areas[i] = '德国';
      } else {
        _areas[i] = _areas[i].trim();
      }
    }

    this.area = _areas.join(',');
    // print('area 修复前[$fixBefore]->修复后[${this.area}]');
  }

  // 修复语言数据
  void fixLang() {
    if (this.lang == null || this.lang == '') {
      return;
    }

    String fixBefore = this.lang;
    this.lang = commonFix(this.lang);

    var _lang = this.lang.split(',');

    for (int i = 0; i < _lang.length; i++) {
      if (_lang[i].contains('英文')) {
        _lang[i] = '英语';
      } else if (_lang[i].contains('汉语') || _lang[i].contains('普通话')) {
        _lang[i] = '汉语';
      } else if (_lang[i] == '法国') {
        _lang[i] = '法语';
      } else if (_lang[i] == '意') {
        _lang[i] = '意大利语';
      } else if (_lang[i] == '西') {
        _lang[i] = '西班牙语';
      } else if (_lang[i] == '韩国') {
        _lang[i] = '韩语';
      } else if (_lang[i] == 'Hindi') {
        _lang[i] = '印地语';
      } else if (_lang[i] == 'Serbian') {
        _lang[i] = '塞尔维亚语';
      } else if (_lang[i] == 'Telugu') {
        _lang[i] = '泰卢固语';
      } else if (_lang[i] == '无对白') {
        _lang[i] = '默片';
      } else if (_lang[i] == '泰文') {
        _lang[i] = '泰语';
      } else if (_lang[i] == '四川话') {
        _lang[i] = '四川方言';
      } else if (_lang[i] == '上海话') {
        _lang[i] = '上海方言';
      } else if (_lang[i].contains('俄语') || _lang[i].contains('俄罗斯')) {
        _lang[i] = '俄语';
      } else if (_lang[i].contains('韩文') || _lang[i].contains('韩语')) {
        _lang[i] = '韩语';
      } else if (_lang[i].contains('其他') || _lang[i].contains('其它') || _lang[i].contains('未知') || _lang[i].contains('未和') || _lang[i] == '未') {
        _lang[i] = '其他';
      } else {
        _lang[i] = _lang[i].trim();
      }
    }

    this.lang = _lang.join(',');
    // print('lang 修复前[$fixBefore]->修复后[${this.lang}]');
  }

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      TableMedia.columnOriginId: originId,
      TableMedia.columnName: name,
      TableMedia.columnSubName: subName,
      TableMedia.columnCover: cover,
      TableMedia.columnDescription: description,
      TableMedia.columnDoubanRank: doubanRank,
      TableMedia.columnUpNum: upNum,
      TableMedia.columnDownNum: downNum,
      TableMedia.columnDirector: director,
      TableMedia.columnActor: actor,
      TableMedia.columnTag: tag,
      TableMedia.columnYear: year,
      TableMedia.columnArea: area,
      TableMedia.columnLang: lang,
      TableMedia.columnPubDate: pubDate,
      TableMedia.columnImdb: imdb,
      TableMedia.columnPlayData: playData,
      TableBase.columnCreateTime: createTime,
    };
    if (id != null) {
      map[TableBase.columnId] = id;
    }
    return map;
  }

  Media.fromMap(Map<String, dynamic> map) {
    id = map[TableBase.columnId];
    originId = map[TableMedia.columnOriginId];
    name = map[TableMedia.columnName];
    subName = map[TableMedia.columnSubName];
    cover = map[TableMedia.columnCover];
    description = map[TableMedia.columnDescription];
    doubanRank = map[TableMedia.columnDoubanRank];
    upNum = map[TableMedia.columnUpNum];
    downNum = map[TableMedia.columnDownNum];
    director = map[TableMedia.columnDirector];
    actor = map[TableMedia.columnActor];
    tag = map[TableMedia.columnTag];
    year = map[TableMedia.columnYear];
    area = map[TableMedia.columnArea];
    lang = map[TableMedia.columnLang];
    pubDate = map[TableMedia.columnPubDate];
    imdb = map[TableMedia.columnImdb];
    playData = map[TableMedia.columnPlayData];

    createTime = DateTime.now().millisecondsSinceEpoch;
  }

  // 将通用爬虫数据转换成业务对象
  Media.fromGeneralSpiderJson(Map<String, dynamic> map) {
    // {list_id: 1, list_name: 电影片}
    originId = int.parse(map['vod_id']);
    name = map['vod_name'];
    subName = map['vod_title'];
    cover = map['vod_pic'];
    description = map['vod_content'];
    categoryId = int.parse(map['vod_cid']); // originId
    doubanId = map['vod_douban_id'];
    upNum = 0;
    downNum = 0;
    director = map['vod_director'];
    actor = map['vod_actor'];
    //tag = map[''];
    year = int.parse(map['vod_year']); // "2009"
    area = map['vod_area']; // \u65e5\u672c
    lang = map['vod_language']; // \u65e5\u8bed
    pubDate = DateTime.parse(map['vod_addtime']).millisecondsSinceEpoch; // 2020-09-01 00:52:52
    // imdb = map[''];
    playData = map['vod_url'];
    createTime = DateTime.now().millisecondsSinceEpoch;
  }

  // @Deprecated
  String mediaName() {
    if (this.name.length > 9) {
      return this.name.substring(0, 9);
    }
    return this.name;
  }

  // 统一分隔规则
  List<String> _splitPlayUrl() {
    List<String> urls = [];
    if (playData.contains('\r\n')) {
      urls = playData.split('\r\n');
    } else if (playData.contains(r'$$$')) {
      urls = playData.split(r'$$$');
    }else {
      return [playData];
    }

    return urls;
  }

  String defaultPlayUrl({int selectedIndex}) {

    final urls = _splitPlayUrl();
    if (urls.length == 0) {
      return ''; // 这里应设置一个默认的播放地址
    }

    String defaultUrl = urls[selectedIndex - 1].split(r'$')[1];
    return defaultUrl;
  }

  // 返回剧集信息
  List<anthology> get anthologyList {
    int i = 0;
    return _splitPlayUrl().map((playInfoStr) {
      // print('playInfoStr:$playInfoStr');
      final playInfo = playInfoStr.split(r'$');
      if (playInfo.length != 2) {
        return null;
      }

      i++;
      return anthology(index: i, title: playInfo[0], playUrl: playInfo[1]);
    }).toList();
  }
}

// 剧集信息。不做数据库记录
class anthology {
  int index;
  String title;
  String playUrl;

  anthology({this.index, this.title, this.playUrl});
}
